import java.util.Arrays;
//自定义数组下标异常
class PosExpection extends RuntimeException{
    public PosExpection(String message){
        super(message);
    }
}
class EmptyException extends RuntimeException{
    public EmptyException(String message){
        super(message);
    }
}
public class MyArrayList implements IList {

    public int[] array ;
    public int useSize;
    public static final int DEFAULT_CAPACITY =10;

    public MyArrayList() {
        this.array=new int[DEFAULT_CAPACITY];
    }

    //对数组进行扩容，将数组容量扩大二倍
    private void grow(){
        array= Arrays.copyOf(this.array,2*array.length);
    }
    @Override
    public void add(int data) {
        //首先需要先判断数组是否已经存满
        if(isFull()){
            grow();
        }
        //
        array[useSize]=data;
        useSize++;
    }

    @Override
    public boolean isFull() {
        //相等返回true；反之，返回false
        return useSize== array.length;
    }
    //这个是私有方法，只是为了在这个类中检查数组下标是否合理，
    // 所以用private修饰
    private void checkPos(int pos) throws PosExpection{
        if(pos < 0 || pos >= useSize){
            throw new PosExpection("数组下标异常");
        }
    }
    @Override
    public void add(int pos, int data) {
      try {
          checkPos(pos);
          //如果插入数组下表没问题，则判断是否需要扩容
          if(isFull()){
              grow();
          }
      }catch (PosExpection e){
          System.out.println("插入数组下标不合理...");
          e.printStackTrace();
      }
      //这里挪动数组，将Pos下标之后的数组往后挪
        for (int i = useSize-1; i >=pos ; i--) {
            array[i+1]= array[i];
        }
        array[pos]=data;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(array[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(array[i]==toFind){
                return i;
            }
        }
        return 0;
    }

    @Override
    public int get(int pos) {
        try {
            checkPos(pos);
            return array[pos];
        }catch (PosExpection e){
            System.out.println("数组下标不合理...");
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkPos(pos);
            array[pos]=value;
        }catch (PosExpection e){
            System.out.println("数组下标不合理...");
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        int pos=indexOf(toRemove);
        if(pos==-1){
            return;
        }
        for (int i = pos; i <useSize-1 ; i++) {
            array[i]=array[i+1];
        }
        useSize--;
    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public void clear() {
         useSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(array[i]+" ");
        }
        //这里不能for-each遍历,
        // 用for-each遍历不管数组里面有没有数据，都会遍历出和数组大小一样的元素，对应下标没有元素会用0来代替。
//        for (int x:
//             array) {
//            System.out.println(x+" ");
//        }
    }
}
